# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
import sys,os

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setMinimumSize(QtCore.QSize(600, 500))
        # 设置主窗口背景颜色
        MainWindow.setStyleSheet("background-color: #fafafa;")
        background = os.path.join('Pictures','background.jpg')
        MainWindow.setStyleSheet("#MainWindow{border-image:url(Pictures/background.jpg)}")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        # 设置icon图标
        filename = self.resource_path(os.path.join('Pictures','icon.ico'))
        MainWindow.setWindowIcon(QtGui.QIcon(filename))
        self.main_layout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.main_layout.setContentsMargins(30, 30, 30, 30)
        self.main_layout.setSpacing(20)

        # 分组布局（策略配置 + 仿真控制）
        self.group_layout = QtWidgets.QHBoxLayout()
        self.group_layout.setSpacing(30)

        # 策略配置分组框 - 浅蓝色背景
        self.groupBox_config = QtWidgets.QGroupBox("策略配置")
        self.groupBox_config.setStyleSheet("""
            QGroupBox {
                border: 1px solid rgba(173, 216, 230, 0.7);
                border-radius: 10px;
                margin-top: 1ex;
                font-size: 16px;
                font-weight: bold;
                color: #333;
                background-color: rgba(224, 240, 248, 0.7);
                padding: 15px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                background-color: rgba(224, 240, 248, 0.9);
                border-radius: 5px;
            }
        """)
        self.config_layout = QtWidgets.QVBoxLayout(self.groupBox_config)
        self.config_layout.setSpacing(15)
        self.btnAttackConfig = self.create_button("配置攻击策略", 220, 55, 14)
        self.btnDefenseConfig = self.create_button("配置防御策略", 220, 55, 14)
        self.config_layout.addWidget(self.btnAttackConfig)
        self.config_layout.addWidget(self.btnDefenseConfig)
        self.group_layout.addWidget(self.groupBox_config)

        # 仿真控制分组框 - 浅红色背景
        self.groupBox_sim = QtWidgets.QGroupBox("仿真初始化")
        self.groupBox_sim.setStyleSheet("""
            QGroupBox {
                border: 1px solid rgba(255, 192, 203, 0.7);
                border-radius: 10px;
                margin-top: 1ex;
                font-size: 16px;
                font-weight: bold;
                color: #333;
                background-color: rgba(255, 230, 235, 0.7);
                padding: 15px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                background-color: rgba(255, 230, 235, 0.9);
                border-radius: 5px;
            }
        """)
        self.sim_layout = QtWidgets.QVBoxLayout(self.groupBox_sim)
        self.sim_layout.setSpacing(15)
        self.btnRflysim = self.create_button("初始化 Rflysim 仿真平台", 220, 55, 14)
        self.btnMatlab = self.create_button("获取无人机轨迹", 220, 55, 14)
        self.sim_layout.addWidget(self.btnRflysim)
        self.sim_layout.addWidget(self.btnMatlab)
        self.group_layout.addWidget(self.groupBox_sim)

        self.main_layout.addLayout(self.group_layout)

        # 分隔线
        line = QtWidgets.QFrame()
        line.setFrameShape(QtWidgets.QFrame.HLine)
        line.setFrameShadow(QtWidgets.QFrame.Sunken)
        line.setStyleSheet("color: rgba(204, 204, 204, 0.7);")
        self.main_layout.addWidget(line)

        # 核心操作按钮（可视化仿真）
        self.btnSimulate = self.create_button("可视化仿真", 320, 70, 16, bold=True, color="#2196F3", text_color="white")
        self.main_layout.addWidget(self.btnSimulate, alignment=QtCore.Qt.AlignCenter)

        # 日志显示区
        self.logBrowser = QtWidgets.QTextBrowser()
        self.logBrowser.setReadOnly(True)
        self.logBrowser.setStyleSheet("""
            QTextBrowser {
                border: 1px solid rgba(204, 204, 204, 0.7);
                border-radius: 10px;
                padding: 10px;
                background-color: rgba(255, 255, 255, 0.8);
            }
        """)
        self.main_layout.addWidget(self.logBrowser)

        # 结束仿真按钮
        self.btnExit = self.create_button("结束仿真", 220, 55, 14, color="#F44336", text_color="white")
        self.main_layout.addWidget(self.btnExit, alignment=QtCore.Qt.AlignCenter)

        # 状态栏
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        MainWindow.setStatusBar(self.statusbar)

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def resource_path(self, relative_path):
        if getattr(sys, 'frozen', False):
            base_path = sys._MEIPASS
        else:
            base_path = "./"
        ret_path = os.path.join(base_path, relative_path)
        return ret_path

    def create_button(self, text, width, height, font_size, bold=False, color="#f0f0f0", text_color="#333"):
        """创建带样式的按钮（移除图标依赖）"""
        btn = QtWidgets.QPushButton()
        btn.setMinimumSize(QtCore.QSize(width, height))
        font = QtGui.QFont()
        font.setPointSize(font_size)
        if bold:
            font.setBold(True)
            font.setWeight(75)
        btn.setFont(font)
        btn.setText(text)

        # 添加图标
        if "攻击" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/attack.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(24, 24))
        elif "防御" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/defense.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(24, 24))
        elif "Rflysim" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/simulation.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(24, 24))
        elif "Matlab" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/matlab.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(24, 24))
        elif "仿真" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/play.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(28, 28))
        elif "结束" in text:
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/exit.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            btn.setIcon(icon)
            btn.setIconSize(QtCore.QSize(24, 24))

        btn.setStyleSheet(f"""
            QPushButton {{
                background-color: {color};
                color: {text_color};
                border-radius: 12px;
                padding: 8px;
                border: none;
                opacity: 0.9;
                box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
            }}
            QPushButton:hover {{
                background-color: {self.darken_color(color)};
                opacity: 1;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
                transform: translateY(-1px);
            }}
            QPushButton:pressed {{
                transform: translateY(1px);
                box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
            }}
        """)
        return btn

    def darken_color(self, color_str):
        """将颜色加深用于按钮悬停效果"""
        color = QtGui.QColor(color_str)
        factor = 0.9
        r = max(0, int(color.red() * factor))
        g = max(0, int(color.green() * factor))
        b = max(0, int(color.blue() * factor))
        return f"#{r:02x}{g:02x}{b:02x}"

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "无人机集群复合攻击与防御仿真平台"))
